import os
import pdb
import requests
import hashlib
from bs4 import BeautifulSoup
import unicodedata

from .student_type import student_type_dic, enter_school_dic, political_face_dic, gender_dic
from . import utility

# http://stu.sz.tsinghua.edu.cn/
# teacher account api
ROOT_URL = 'http://stu.sz.tsinghua.edu.cn/'
QUERY_BATCH_URL = ROOT_URL + 'xg/xsxx.action'

NUM_PAGE = 100

def remove_control_characters(s):
    return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")

def login(username=None, password=None):
    if(username == None):
        username = os.environ.get('sz_username', 'user')
    if(password == None):
        password = os.environ.get('sz_password', 'pass')
    LOGIN_URL = ROOT_URL + 'j_spring_security_check'
    password_md5 = hashlib.md5(password.encode('ascii')).hexdigest()
    login_data = {'j_username': username, 'j_password': password_md5, 'utype': 'A', "password": password}
    session = requests.Session()
    r = session.post(LOGIN_URL, data=login_data)
    if(r.status_code == 200 and r.url.find('index.jsp') > 0):
        return session
    print(r.text)
    return None

def query_batch(session, filter_dic):
    # currently only support id filter
    r = session.post(QUERY_BATCH_URL, data=filter_dic)
    if(r.text.find('id=') > 0):
        return parse_html_batch(r.text)
    return []

def get_total_number(content):
    soup = BeautifulSoup(content)
    st = soup.find(class_='pagination').get('totalcount')
    try:
        st_int = int(st)
    except Exception as e:
        pdb.set_trace()
    return st_int
    
def query_all(session, filter_dic, cursor):
    r = session.post(QUERY_BATCH_URL, data=filter_dic)
    if(r.text.find('id=') < 0):
        return False
    total_number = get_total_number(r.text)
    student_list = parse_html_batch(r.text)
    utility.insert_list(student_list, cursor)
    
    filter_dic_inner = filter_dic.copy()
    filter_dic_inner['numPerPage'] = NUM_PAGE
    max_pageNum = int(total_number/NUM_PAGE) + 1
    for i in range(2,max_pageNum+1):
        filter_dic_inner['pageNum'] = i
        student_list = query_batch(session, filter_dic_inner)
        utility.insert_list(student_list, cursor)
    return True

def update_student_info(session, student_id_list, cursor, verbose=True):
    # update student info from the remote server
    len_20 = int(len(student_id_list)/20)
    counter = 0
    outer_counter = 0
    for i in student_id_list:
        dic = query(session, i)
        utility.update(cursor, dic)
        counter += 1
        if(verbose and counter == len_20):
            counter = 0
            outer_counter += 1
            print('%d/20'%outer_counter)
            
def query(session, student_id):
    inner_student_id = student_id # compatible with old student_id format
    if(student_id[:1] == '11'):
        inner_student_id = student_id[2:]
    query_dict = { 'id' : inner_student_id }
    QUERY_URL = ROOT_URL + 'xg/xsxx!input.action'
    r = session.get(QUERY_URL, params=query_dict)
    if(r.text.find(inner_student_id) > 0):
        return parse_html(r.text)
    return {}

def parse_html_batch(content):
    student_list = []
    soup = BeautifulSoup(content)
    for tr in soup.find(class_='pageContent').find_all('tr'):
        td_list = tr.find_all('td')
        if(len(td_list) == 0):
            continue
        dic = {}
        try:
            dic['student_id'] = td_list[1].text.lstrip().rstrip()
            dic['student_name'] = td_list[2].text.lstrip().rstrip()
            dic['student_gender'] = gender_dic[td_list[3].text]
            dic['student_id_card'] = td_list[4].text
            dic['student_type'] = student_type_dic[td_list[9].text.lstrip().rstrip()]
            dic['student_political_face'] = political_face_dic[td_list[10].text]
            dic['student_institute'] = td_list[11].text.lstrip().rstrip()            
            dic['student_major'] = td_list[12].text.lstrip().rstrip()  
            dic['student_mentor_name'] = td_list[15].text.lstrip().rstrip()  
            dic['student_enter_method'] = enter_school_dic[td_list[16].text]
        except Exception as e:
            pdb.set_trace()
        student_list.append(dic)
    return student_list
    
def parse_html(content):
    dic = {}
    soup = BeautifulSoup(content)
    # hard coded index offset
    input_list = soup.find_all('input')
    try:
        dic['student_id'] = input_list[0].get('value')        
        dic['student_name'] = input_list[1].get('value')
        if(dic['student_name'] == ''):
            return {}
        dic['student_gender'] = gender_dic[input_list[2].get('value')]
        dic['student_id_card'] = input_list[3].get('value')
        # dic['student_type'] = input_list[4].get('value')
        dic['student_major'] = input_list[6].get('value')
        dic['student_class'] = input_list[7].get('value')
        dic['student_group_leader'] = input_list[10].get('value')
        dic['student_mentor_name'] = input_list[12].get('value')
        if(input_list[14].get('value') != ''):
            dic['student_political_face'] = political_face_dic[input_list[14].get('value')]
        dic['student_nationality'] = input_list[19].get('value')
        dic['student_relative_name'] = input_list[22].get('value')
        dic['student_relative_phone'] = input_list[24].get('value')
        dic['student_undergraduate_university'] = input_list[25].get('value')
        dic['student_undergraduate_major'] = input_list[26].get('value')
        # dic['student_email'] = input_list[28].get('value')
        dic['student_mobilephone'] = input_list[29].get('value')
        dormitory_str = utility.clean_dormitory(input_list[36].get('value'))
        if(dormitory_str is not None and len(dormitory_str) < 10 and len(dormitory_str) > 0):
            dic['student_dormitory'] = dormitory_str
        dic['student_house_address'] = remove_control_characters(input_list[38].get('value'))
    except Exception as e:
        pdb.set_trace()
    return dic

if __name__ == '__main__':
    username = os.environ.get('sz_username', 'user')
    password = os.environ.get('sz_password', 'pass')
    session = login(username, password)
    if(session != None):
        dic = query(session, '2017310711')
        print(dic)